﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZX.HIS.Common;

namespace ZX.HIS.UI.Controllers
{
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult addUserInfo()
        {
            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            //获取科室信息
            ZX.HIS.BLL.DepartInfo bll = new BLL.DepartInfo();
            ViewBag.DepartId = new SelectList(bll.AllDepartInfo(HospitalId), "DepartId", "DepartName");

            //权限显示
            ZX.HIS.BLL.UserInfo bll2 = new BLL.UserInfo();
            List<ZX.HIS.Model.AuthorInfo> list1 = bll2.allAuthorInfo();
            ViewBag.AuthorId = list1;
            return View();
        }

        public ActionResult updaUserInfo(int id)
        {
            ZX.HIS.BLL.UserInfo bll = new BLL.UserInfo();

            //  人员，权限派生表信息
            List<ZX.HIS.Model.AuthorUserInfo> list2 = bll.allAuthorUserInfo();
            ViewBag.AuthorUserInfo = list2;
            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            //获取科室信息
            ZX.HIS.BLL.DepartInfo bll1 = new BLL.DepartInfo();
            ViewBag.DepartId = new SelectList(bll1.AllDepartInfo(HospitalId), "DepartId", "DepartName", id);
            //权限显示

            List<ZX.HIS.Model.AuthorInfo> list1 = bll.allAuthorInfo();
            ViewBag.AuthorId = list1;

            //返填

            List<ZX.HIS.Model.UserInfo> list = bll.showUserInfo();
            ZX.HIS.Model.UserInfo model = list.Where(p => p.UserId == id).FirstOrDefault();
            ViewBag.UserInfo = model;

            ViewBag.UserId = id;
            return View();
        }

        /// <summary>
        ///  人员表分页显示
        /// </summary>
        /// <returns></returns>
        public ActionResult Fnpagerdo()
        {
            ZX.HIS.BLL.UserInfo bll = new BLL.UserInfo();
            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            int pageindex = Convert.ToInt32(Request["pageindex"]);
            int pagesize = Convert.ToInt32(Request["pagesize"]);
            PageClass<ZX.HIS.Model.UserInfo> p = bll.Pageuserinfo(pageindex, pagesize,HospitalId);
            return Json(p);
        }
        /// <summary>
        /// 人员添加
        /// </summary>
        /// <returns></returns>
        public int FnaddUserInfodo()
        {
            string AuthorId = (Request["AuthorId"]).ToString();

            ZX.HIS.BLL.UserInfo bll = new BLL.UserInfo();

            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            string UserName = Request["UserName"].ToString();
            string UserPwd = Request["UserPwd"].ToString();
            string UserChineseName = Request["UserChineseName"].ToString();
            int UserSex = Convert.ToInt32(Request["UserSex"]);
            string UserPhone = Request["UserPhone"].ToString();
            string UserEmail = Request["UserEmail"].ToString();
            int DepartId = Convert.ToInt32(Request["DepartId"]);

            int UserSecrecy = Convert.ToInt32(Request["UserSecrecy"]);
            int UserState = Convert.ToInt32(Request["UserState"]);
            ZX.HIS.Model.UserInfo user = new Model.UserInfo();
            user.HospitalId = HospitalId;
            user.UserName = UserName;
            user.UserPwd = UserPwd;
            user.UserChineseName = UserChineseName;
            user.UserSex = UserSex;
            user.UserPhone = UserPhone;
            user.UserEmail = UserEmail;
            user.DepartId = DepartId;
            user.UserSecrecy = UserSecrecy;
            user.UserState = UserState;
            int i = bll.addtbUserInfo(user);
            if (i > 0)
            {
                return bll.addtbAuthorUserInfo(i, AuthorId);
            }
            else
            {
                return 0;
            }

        }

        public int FnupdaUserInfodo()
        {
            ZX.HIS.BLL.UserInfo bll = new BLL.UserInfo();

            string AuthorId = (Request["AuthorId"]).ToString();

            string UserPwd = Request["UserPwd"].ToString();
            string UserChineseName = Request["UserChineseName"].ToString();
            int UserSex = Convert.ToInt32(Request["UserSex"]);
            string UserPhone = Request["UserPhone"].ToString();
            string UserEmail = Request["UserEmail"].ToString();
            int DepartId = Convert.ToInt32(Request["DepartId"]);

            int UserSecrecy = Convert.ToInt32(Request["UserSecrecy"]);
            int UserState = Convert.ToInt32(Request["UserState"]);

            int UserId = Convert.ToInt32(Request["UserId"]);

            ZX.HIS.Model.UserInfo model = new Model.UserInfo();
            model.UserPwd = UserPwd;
            model.UserChineseName = UserChineseName;
            model.UserSex = UserSex;
            model.UserPhone = UserPhone;
            model.UserEmail = UserEmail;
            model.DepartId = DepartId;
            model.UserSecrecy = UserSecrecy;
            model.UserState = UserState;
            model.UserId = UserId;
            if (bll.updaUserInfo(model) > 0)
            {
                return bll.addtbAuthorUserInfo(UserId, AuthorId);
            }
            else
            {
                return 2;
            }
        }

        public int FndelUserInfo()
        {
            ZX.HIS.BLL.UserInfo bll = new BLL.UserInfo();
            int UserId = Convert.ToInt32(Request["UserId"]);
            int state = Convert.ToInt32(Request["state"]);
            if (bll.delUserInfo(state, UserId) > 0)
            {
                return 1;
            }
            else
            {

                return 0;
            }
        }
    }
}
